package com.leyou.auth.service;

import com.leyou.auth.clients.UserClient;
import com.leyou.auth.config.JwtProperties;
import com.leyou.auth.entiy.UserInfo;
import com.leyou.auth.utils.JwtUtils;
import com.leyou.user.pojo.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AuthService {

    @Autowired
    private UserClient userClient;

    static final Logger logger = LoggerFactory.getLogger(AuthService.class);

    public String accredit(String username, String password, JwtProperties prop) {

        //调用user-service 查看用户是否正确
        try {
            //查询用户
            User user = userClient.queryUser(username, password);
            //判断用户是否存在
            if (user==null){
                logger.info("用户信息不存咋：{}",username);
                return null;
            }

            //生成token
            String token = JwtUtils.generateToken(new UserInfo(user.getId(),user.getUsername()),prop.getPrivateKey(),prop.getExpire());
            return token;
        } catch (Exception e) {
            return null;
        }


    }
}
